<?php
/******************************************
 * Author: <YOUR NAME HERE>
 * Description: <YOUR DESCRIPTION HERE>
 ******************************************/
class Matches  extends CustomSecurity { 

	var $db; 
	var $date;
	var $hour;
	var $matchID;
	var $teamA;
	var $teamB;
	var $resultA;
	var $resultB;
	var $stage;
	
	function Matches($db){ 
          $this->db = $db; 
    }
//
//	/***********************************
// 	 * Add:         Allows the insertion of values into the Table.
// 	 * Parameters:  $date, $hour, $matchID, $teamA, $teamB, $resultA, $resultB, $stage
// 	 * Return:      (Boolean) True - Successfully Inserted | False - Error
// 	 ************************************/
//	function add($date, $hour, $matchID, $teamA, $teamB, $resultA, $resultB, $stage){ 
//
//		$statement = "INSERT INTO matches (date, hour, matchID, teamA, teamB, resultA, resultB, stage) VALUES ('$date', '$hour', '$matchID', '$teamA', '$teamB', '$resultA', '$resultB', '$stage');";
//		$results   = $this->db->query($statement);
//
//		if($results){
//			return true;
//		}else{
//			return false;
//		}
//
//	}//End add()
//
//
//	/***********************************
// 	 * Remove:      Allows for the removal of record from the database.
// 	 * Parameters:  (String)Primary Key Column Name | (Int) Primary Key Value
// 	 * Return:      (Boolean) True - Successfully Removed | False - Error
// 	 ************************************/
//	function remove($primaryKeyName, $primaryKeyValue){ 
//
//		//Custom SQL Injection Escaping HERE
//
//		$statement = "DELETE FROM matches WHERE $primaryKeyName = '$primaryKeyValue'";
//		$results   = $this->db->query($statement);
//		if($results){
//			return true;
//		}else{
//			return false;
//		}
//
//	}//End remove()
//

	/***********************************
 	 * Update:      Allows for the update of a record from the database.
 	 * Parameters:  (String)Primary Key Column Name | (Int) Primary Key Value | (String) Column Name To Update | (String) New Value
 	 * Return:      (Boolean) True - Successfully Updated | False - Error
 	 ************************************/
	function update($id, $resA, $resB){ 

		escape_string($id);
		escape_string($resA);
		escape_string($resB);
		
		$statement = "UPDATE matches SET resultA = '$resA',  resultB = '$resB' WHERE matchID = '$id'";
		$results   = $this->db->query($statement);
		if($results){
			return true;
		}else{
			return false;
		}

	}//End update()


	/***********************************
 	 * GetAll:       Returns all the records in the database.
 	 * Parameters:   NA
 	 * Return:      (MultiDimensional-Array)String
 	 ************************************/
	function getAll(){ 

		$statement = "SELECT matchID, date, teamA, resultA, resultB, teamB, stage, p.progA, p.progB, m.stage FROM matches m LEFT OUTER JOIN prognosis p ON matchID=machID AND authID=".$_SESSION['user_id']."  ORDER BY date ASC";
		$results   = $this->db->getAll($statement);

		return $results;

	}//End getAll()
	
	function getAllFinished(){ 

		$statement = "SELECT matchID, resultA, resultB FROM matches WHERE resultA != -1";
		$results   = $this->db->getAll($statement);

		return $results;

	}
	
	function get($id){ 

		//Custom SQL Injection Escaping HERE

		$statement = "SELECT date FROM matches WHERE matchID=$id";
		$results   = $this->db->getAll($statement);

		return $results;

	}//End getAll()
	
	
	function computeRanking($group){
		$result = array();
		$statement = "SELECT resultA, resultB, teamA, teamB FROM matches WHERE stage='$group'";
		$stateRes   = $this->db->getAll($statement);
		$size = count($stateRes);
		for ($i = 0; $i < $size; $i++) {
			$resultA = $stateRes[$i][0];
			$resultB = $stateRes[$i][1];
			$teamA = $stateRes[$i][2];
			$teamB = $stateRes[$i][3];
			if ($resultA >= 0){
				if($resultA == $resultB){
					$this->updateScore($teamA, 1, $resultA, $resultA, &$result);
					$this->updateScore($teamB, 1, $resultA, $resultA, &$result);
				} else if ($resultA > $resultB){
					$this->updateScore($teamA, 3, $resultA, $resultB, &$result);
					$this->updateScore($teamB, 0, $resultB, $resultA, &$result);
				} else {
					$this->updateScore($teamA, 0, $resultA, $resultB, &$result);
					$this->updateScore($teamB, 3, $resultB, $resultA, &$result);
				}
			}
		}
		usort($result, array("Matches", "cmp"));
		return $result;
	}
	
	static function cmp($a, $b){
//		echo $a.' < '.$b[0].'|'.$a[6].'|'.$b[6].'<br/>';
		if($a[6] < $b[6]){
			return 1;
		} else if($a[6] > $b[6]){
			return -1;
		}
		if( ($a[4]-$a[5]) < ($b[4]-$b[5])){
			return 1;
		} else if( ($a[4]-$a[5]) > ($b[4]-$b[5])){
			return -1;
		}
		if($a[4] < $b[4]){
			return 1;
		} else if($a[4] > $b[4]){
			return -1;
		}
		return 0;
	}
	
	

	function updateScore($team, $points, $goals, $goalsIn, &$result){
		$index = 0;
		$size = count(&$result);
		if($size > 0){
			for ($i = 0; $i <= $size; $i++) {
				if(strcmp(&$result[$i][0], $team) == 0){
					$index = $i;
					break;
				}
				$index = $i;
			}	
		}
		
		$result[$index][0] = $team;
		$wins = 0;
		$losses = 0;
		$draw = 0;
		if($points == 3){
			$wins = 1;
		} else if($points == 1){
			$draw = 1;
		} else {
			$losses = 1;
		}
		if (isset($result[$index][1])){
			$result[$index][1] = $result[$index][1]+$wins;
			$result[$index][2] = $result[$index][2]+$losses;
			$result[$index][3] = $result[$index][3]+$draw;
			$result[$index][4] = $result[$index][4]+$goals;
			$result[$index][5] = $result[$index][5]+$goalsIn;
			$result[$index][6] = $result[$index][6]+$points;
		} else {
			$result[$index][1] = $wins;
			$result[$index][2] = $losses;
			$result[$index][3] = $draw;
			$result[$index][4] = $goals;
			$result[$index][5] = $goalsIn;
			$result[$index][6] = $points;
		}
		
	}
	
	function getAllClean(){
		
		escape_string($id);

		$statement = "SELECT matchID, date, hour, teamA, resultA, resultB, teamB, stage FROM matches";
		$results   = $this->db->getAll($statement);

		return $results;
	}
	
	function getCountries(){ 

		$statement = "SELECT DISTINCT teamA FROM matches";
		$results   = $this->db->getAll($statement);

		return $results;

	}//End getAll()


	/***********************************
 	 * GetObject:   Returns a specific record form the batabase.
 	 * Parameters:  (Int) Primary Key Value
 	 * Return:      (Array)String
 	 ************************************/
	function getObject($primaryKeyValue){

		escape_string($primaryKeyValue);

		$statement = "SELECT matchID, teamA, teamB, resultA, resultB, stage FROM matches WHERE matchID = '".$primaryKeyValue."'";
		$results   = $this->db->getAll($statement);

		return $results;

	}//End getObject()


}//End Class Matches
?>